package net.huashitong.ssydt.provider.pk.service;


import net.huashitong.ssydt.base.dao.LongMapper;
import net.huashitong.ssydt.base.service.AbstractLongService;
import net.huashitong.ssydt.pk.constant.PkConsts;
import net.huashitong.ssydt.pk.entity.SsPkGameRule;
import net.huashitong.ssydt.pk.entity.SsPkYzddMatchRecord;
import net.huashitong.ssydt.provider.pk.mapper.SsPkYzddMatchRecordMapper;
import net.huashitong.ssydt.provider.utils.date.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.12.18
 */
@Service
@Transactional
public class SsPkYzddMatchRecordServiceImpl extends AbstractLongService<SsPkYzddMatchRecord> implements SsPkYzddMatchRecordService {

    private SsPkYzddMatchRecordMapper mapper;

    private SsPkGameRuleService ssPkGameRuleService;

    @Autowired
    public SsPkYzddMatchRecordServiceImpl(SsPkYzddMatchRecordMapper mapper,
                                          SsPkGameRuleService ssPkGameRuleService) {
        this.mapper = mapper;
        this.ssPkGameRuleService = ssPkGameRuleService;
    }

    @Override
    protected LongMapper<SsPkYzddMatchRecord> getLongMapper() {
        return this.mapper;
    }

    @Override
    public SsPkYzddMatchRecord getNowYzddMatch() {
        SsPkYzddMatchRecord ssPkYzddMatchRecord = this.mapper.getNotHistory();
        if (ssPkYzddMatchRecord == null
                || DateUtils.getDateTime().compareTo(ssPkYzddMatchRecord.getEndTime()) > 0) {
            if (ssPkYzddMatchRecord != null) {
                ssPkYzddMatchRecord.setHistoryId(ssPkYzddMatchRecord.getId());
                this.updateByPrimaryKey(ssPkYzddMatchRecord);
            }

            String startTime = getStartDateTime();
            String endTime = getEndDateTime();
            SsPkGameRule ssPkGameRule = ssPkGameRuleService.getByType(PkConsts.RuleType.YZDD);

            ssPkYzddMatchRecord = new SsPkYzddMatchRecord();
            BeanUtils.copyProperties(ssPkGameRule, ssPkYzddMatchRecord);
            ssPkYzddMatchRecord.setStartTime(startTime);
            ssPkYzddMatchRecord.setEndTime(endTime);
            ssPkYzddMatchRecord.setHistoryId(0L);
            ssPkYzddMatchRecord.setIsFinal((byte) 0);
            ssPkYzddMatchRecord.setIsSettlement((byte) 0);
            this.insertSelective(ssPkYzddMatchRecord);
        }
        return ssPkYzddMatchRecord;
    }

    @Override
    public Long getLastMatchRecordId(Long matchRecordId) {
        return this.mapper.getLastMatchRecordId(matchRecordId);
    }

    @Override
    public List<SsPkYzddMatchRecord> findNonFinal() {
        return this.mapper.findNonFinal(this.getNowYzddMatch().getId());
    }

    /**
     * 获取开始时间
     *
     * @return
     */
    private String getStartDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取结束时间
     *
     * @return
     */
    private String getEndDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.SECOND, -1);
        return sdf.format(calendar.getTime());
    }


}
